<?php

namespace api\modules\v1\controllers\patient;

use common\helpers\CommonHelper;
use services\linkedcare\LinkedcareChannelService;
use services\patient\PatientTypeService;
use Yii;
use api\controllers\OnAuthController;
use yii\web\HttpException;

class PatientTypeController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/patient/patient-type/index 患者类型列表
     * @apiGroup PatientType
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list             列表数据
     * @apiSuccess {Number}  data.list.id               列表ID
     * @apiSuccess {String}  data.list.type             类型
     * @apiSuccess {String}  data.list.name             名称
     * @apiSuccess {Number}  data.list.status           状态
     * @apiSuccess {String}  data.list.status_name      状态名称
     * @apiSuccess {String}  data.list.source_name      E看牙渠道
     * @apiSuccess {Number}  data.list.sort             排序
     * @apiSuccess {Number}  data.condition             渠道筛选【三级】
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $commonService = new PatientTypeService();
        $list = $commonService->patientTypeList($params, $page, $pageSize);
        $condition = LinkedcareChannelService::getChannelCondition(1);
        return [
            'list' => $list,
            'condition' => $condition,
        ];
    }

    /**
     * @api {post} /v1/patient/patient-type/update-patient-type 更新患者类型
     * @apiGroup PatientType
     * @apiName update-patient-type
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                  ID【编辑时必传】
     * @apiBody {String}   name                名称
     * @apiBody {int}      channel_id          E看牙渠道ID
     * @apiBody {Number}   sort                排序
     * @apiBody {Array}    status              状态【0：禁用; 1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdatePatientType()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '名称必填');
        }
        if (!isset($params['channel_id']) || !$params['channel_id']) {
            return CommonHelper::api(422, '渠道必选');
        }
        $params['id'] = $params['id'] ?? 0;
        $commonService = new PatientTypeService();
        return $commonService->updatePatientType($params);
    }

    /**
     * @api {post} /v1/patient/patient-type/delete-patient-type 删除患者类型（软删除）
     * @apiGroup PatientType
     * @apiName delete-patient-type
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeletePatientType()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $commonService = new PatientTypeService();
        return $commonService->deletePatientType($params);
    }
}
